var express = require('express');
var Domains = require('../modules/domains')
var router = express.Router();

// 添加
router.post('/', function (req, res) {
  var { money, reason, desc } = req.body;
  console.log(req);
  var domains = new Domains({ money, reason, desc })
  domains.save(function (err) {
    if (err) {
      res.json({
        success: false,
        message: "录入失败！"
      })
    }
  })
  res.json({ success: true, message: "录入成功！" })
})

// 查看
router.get('/', function (req, res) {
  Domains.find({}, function (err, categories) {
    res.json({
      success: true,
      data: categories
    })
  })
})

// 更新
router.put('/', function (req, res) {

  // 解构赋值
  var { title, newTitle } = req.body;
  Domains.findOneAndUpdate({ title: title }, { title: newTitle }, function (err, domains) {
    if (err) {
      res.json({
        success: false, message: "更新失败"
      })
    }
  })
  res.json({ success: true, message: "更新成功！" })
})

// 删除
router.delete('/', function (req, res) {
  console.log('delete domains');

  // 解构赋值
  var { title } = req.body;

  Domains.remove({ title: title }, function (err) {
    if (err) {
      res.json({
        success: false, messge: "删除失败！"
      })
    }
  })
  res.json({ success: true, message: "删除成功！" })
})

module.exports = router;